package com.java8.lambda;

import org.junit.Test;

import java.util.*;

public class TestLambda {

    //原来的匿名内部类
    @Test
    public void test1() {
        Comparator<Integer> integerComparator = new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                return Integer.compare(o1, o2);
            }
        };

    }

    //Lambda表达式
    @Test
    public void test2() {
        Comparator<Integer> comparator = (x, y) -> Integer.compare(x, y);
        TreeSet<Integer> treeSet = new TreeSet<>(comparator);
    }


    //需求:获取当前公司种年龄大于35的员工信息

    List<Employee> employeesList = Arrays.asList(new Employee("张三", 18, 9999.99),
            new Employee("李四", 38, 5555.55),
            new Employee("王五", 50, 6666.66));

    @Test
    public void test3() {
        List<Employee> employees = Arrays.asList(new Employee("张三", 18, 9999.99),
                new Employee("李四", 38, 5555.55),
                new Employee("王五", 50, 6666.66));
        //将公司的员工整合在一起.
        for (Employee employee : employees) {
            if (employee.getAge() >= 35) {
                System.out.println(employee);
            }
        }
    }

    //需求:获取当前公司中员工工资大于5000的员工信息
    //优化方式一:策略设计模式
    @Test
    public void test4() {
        List<Employee> employees = Arrays.asList(new Employee("张三", 18, 9999.99),
                new Employee("李四", 38, 5555.55),
                new Employee("王五", 50, 6666.66));
        List<Employee> employees1 = filterEmployee(employees, new FilterEmployeeByAge());
        System.out.println(employees1);
    }

    public List<Employee> filterEmployee(List<Employee> list, MyPredicate<Employee> myPredicate) {
        List<Employee> employees = new ArrayList<>();
        for (Employee employee : list) {
            if (myPredicate.test(employee)) {
                employees.add(employee);
            }
        }
        return employees;
    }

    //优化方式二:匿名内部类
    @Test
    public void test5() {
        List<Employee> employees = filterEmployee(this.employeesList, new MyPredicate<Employee>() {
            @Override
            public boolean test(Employee employee) {
                return employee.getSalary() >= 1111.11;
            }
        });

        for (Employee employee : employees) {
            System.out.println(employee);
        }
    }

    //优化方式三:
    @Test
    public void test6() {
        List<Employee> employees = filterEmployee(this.employeesList, Employee -> {
            return Employee.getAge() >= 35;
        });

        employees.forEach(System.out::println);
    }

    //优化方式四:
    @Test
    public void test7() {
        employeesList.stream().filter(employee -> employee.getSalary() >= 5000).limit(2).forEach(System.out::println);

        System.out.println("-------------------------------");
        employeesList.stream().map(Employee::getAge).forEach(System.out::println);
    }







}
